/*
* @Description:组织管理-新增
* @Author: lil
* @Date: 2021/11/24
**/
<template>
  <div>
    <!-- 表单 -->
    <el-form :model="form" :rules="rules" ref="form" label-width="83px">
      <el-form-item label="组织名称:" prop="name">
        <el-input v-model="form.name" placeholder="请输入组织名称，如Org1" maxlength="6"></el-input>
      </el-form-item>
      <el-form-item label="域名:" prop="domain">
        <el-input v-model="form.domain" placeholder="请输入域名"></el-input>
      </el-form-item>
      <el-form-item label="选择类型:" prop="type">
        <el-select v-model="form.type" placeholder="请选择" @change="handleSelect">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="主机:" prop="hosts">
        <template v-if="addData.length>0">
          <el-checkbox-group v-model="form.hosts">
            <el-checkbox v-for="(item,index) in addData" :key="index" :label="item" @change='selectData(item)'>
            </el-checkbox>
          </el-checkbox-group>
        </template>
        <template v-else>
          暂无主机可选
        </template>
      </el-form-item>
      <el-form-item label-width="0" class="btns">
        <el-button type="primary" :disabled="addData.length==0" v-if="title=='新增'" @click="submitForm('0')">立即创建
        </el-button>
        <el-button type="primary" v-else @click="submitForm('1')">修改</el-button>
        <el-button type="info" @click="resetForm('form')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import {
    addOrg,
    alertOrg
  } from '@/api/blockchain/deployorg'
  import {
    hostListForOrg
  } from '@/api/blockchain/deployhost'
  export default {
    data() {
      var validatePass1 = (rule, value, callback) => {
        let regex = /[^\w]/g;
        if (value == '') {
          return callback(new Error("组织名称不能为空！"));
        } else if (!regex.test(value)) {
          callback();
        } else {
          return callback(new Error("请输入合法的组织名称，字母加数字！"));
        }
      };
      var validatePass2 = (rule, value, callback) => {
        let regex =
          /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/;
        if (value == '') {
          return callback(new Error("域名不能为空！"));
        } else if (regex.test(value)) {
          callback();
        } else {
          return callback(new Error("请输入合法的域名！"));
        }
      };
      return {
        form: {
          name: "",
          domain: "",
          type: "",
          hosts: [],
        },
        oldForm: {
          name: "",
          domain: "",
          type: "",
          hosts: [],
        },
        arr: [],
        rules: {
          name: [{
            required: true,
            validator: validatePass1,
            trigger: "change",
          }, ],
          domain: [{
            required: true,
            validator: validatePass2,
            trigger: "change",
          }, ],
          type: [{
            required: true,
            message: '请选择组织类型！',
            trigger: 'change'
          }],
          hosts: [{
            type: 'array',
            required: true,
            message: '请至少选择一个主机！',
            trigger: 'change'
          }],
        },
        checboxlist: [],
        // 组织类型
        options: [{
            value: "peer",
            label: "peer",
          },
          {
            value: "orderer",
            label: "orderer",
          },
        ],
        defaultType: 'orderer',
        addData: []
      };
    },
    props: {
      org: {
        type: Object
      },
      title: {
        type: String
      }
    },
    mounted() {
      this.initData();
    },
    methods: {
      initData() {
        if (this.org.name != '') {
          this.defaultType = this.org.type;
          this.oldForm = JSON.parse(JSON.stringify(this.org));
        }
        this.form = JSON.parse(JSON.stringify(this.org));
        hostListForOrg(this.defaultType, this.title == '新增' ? 0 : 1, this.org.name == '' ? '' : this.org.name).then((
          res) => {
          this.addData = res;
        })
      },
      handleSelect(val) {
        hostListForOrg(val, this.title == '新增' ? 0 : 1, this.org.name == '' ? '' : this.org.name).then((res) => {
          this.addData = res;
        })
        this.defaultType = val == 'orderer' ? 'orderer' : 'peer';
      },
      selectData(val) {
        if (this.arr.indexOf(val.mark) == -1) {
          this.arr.push(val.mark)
        } else {
          this.arr.splice(this.arr.indexOf(val.mark), 1)
        }
      },
      submitForm(val) {
        if (val == 0) {
          this.$refs.form.validate((valid) => {
            if (valid) {
              var _this = this;
              addOrg(_this.form).then(() => {
                  this.$message({
                    type: 'success',
                    message: '添加组织成功'
                  });
                  this.$emit('closeadd')
                })
                .catch(function (error) {

                })
            }
          })
        } else {
          this.$refs.form.validate((valid) => {
            if (valid) {
              var _this = this;
              alertOrg(_this.oldForm, _this.form).then(() => {
                  this.$message({
                    type: 'success',
                    message: '修改组织成功'
                  });
                  this.$emit('closeadd')
                })
                .catch(function (error) {

                })
            }
          })
        }
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    },
  };

</script>
<style lang="scss" scoped>
  .el-form {
    padding: 40px 40px 10px;

    &>>>.el-form-item__label {
      &::before {
        content: "*";
        display: inline-block;
        color: #f12945;
      }
    }

    .btns {
      text-align: center;
      margin-top: 70px;

      .el-button:nth-of-type(1) {
        margin-right: 40px;
      }
    }
  }

</style>
